package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.UserRoleCompanyAddDTO;
import org.jsola.hr.dto.UserRoleCompanyBatchDTO;
import org.jsola.hr.dto.UserRoleCompanyUpdateDTO;
import org.jsola.hr.query.UserRoleCompanyQuery;
import org.jsola.hr.service.IUserRoleCompanyService;
import org.jsola.hr.vo.UserRoleCompanyListVO;
import org.jsola.hr.vo.UserRoleCompanyVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * 人员分管用工单位
 *
 * @author zhr
 */
@RestController("hrUserRoleCompanyController")
@RequestMapping(HrConstants.API_PATH + "/v1/userRoleCompanies")
public class UserRoleCompanyController implements IUserRoleCompanyControllerAPI {

    @Autowired
    private IUserRoleCompanyService userRoleCompanyService;

    @Override
    @PostMapping
    public Result<UserRoleCompanyVO> save(@Valid @RequestBody UserRoleCompanyAddDTO userRoleCompanyAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增人员分管用工单位权限校验

        return Result.success(
                userRoleCompanyService.save(userRoleCompanyAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody UserRoleCompanyUpdateDTO userRoleCompanyUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改人员分管用工单位权限校验

        int count = userRoleCompanyService.update(userRoleCompanyUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


    @Override
    @PostMapping(value = "/batch")
    public Result batch(@Valid @RequestBody UserRoleCompanyBatchDTO userRoleCompanyBatchDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量操作权限校验

        // 批量操作，注意校验，有越权风险
        // 新增
        List<UserRoleCompanyAddDTO> userRoleCompanyAddDTOList = userRoleCompanyBatchDTO.getUserRoleCompanyAddDTOList();
        if (!CollectionUtils.isEmpty(userRoleCompanyAddDTOList)) {
            userRoleCompanyService.batchSave(userRoleCompanyAddDTOList,tokenUser);
        }
        // 修改
        List<UserRoleCompanyUpdateDTO> userRoleCompanyUpdateDTOList = userRoleCompanyBatchDTO.getUserRoleCompanyUpdateDTOList();
        if (!CollectionUtils.isEmpty(userRoleCompanyUpdateDTOList)) {
            userRoleCompanyUpdateDTOList.parallelStream().forEach(
                    userRoleCompanyUpdateDTO -> {
                        userRoleCompanyService.update(userRoleCompanyUpdateDTO,tokenUser);
                    }
            );
        }
        // 删除
        List<String> deleteUserRoleCompanyIdList = userRoleCompanyBatchDTO.getDeleteUserRoleCompanyIdList();
        if (!CollectionUtils.isEmpty(deleteUserRoleCompanyIdList)) {
            userRoleCompanyService.deleteByIds(tokenUser,deleteUserRoleCompanyIdList.toArray(new String[0]));
        }
        return Result.success();
    }


    @Override
    @DeleteMapping(value = "/{userRoleCompanyId}")
    public Result<Integer> delete(@PathVariable String userRoleCompanyId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除人员分管用工单位权限校验

        int count = userRoleCompanyService.deleteByIds(tokenUser, userRoleCompanyId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] userRoleCompanyIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除人员分管用工单位权限校验

        int count = userRoleCompanyService.deleteByIds(tokenUser, userRoleCompanyIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{userRoleCompanyId}")
    public Result<UserRoleCompanyVO> findById(@PathVariable String userRoleCompanyId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看人员分管用工单位权限校验

        return Result.success(
            userRoleCompanyService.selectById(userRoleCompanyId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<UserRoleCompanyListVO>> listAll(@Valid UserRoleCompanyQuery userRoleCompanyQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看人员分管用工单位权限校验

        return Result.success(
            userRoleCompanyService.select(userRoleCompanyQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<UserRoleCompanyListVO>> page(@Valid UserRoleCompanyQuery userRoleCompanyQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            userRoleCompanyService.selectPage(userRoleCompanyQuery, tokenUser.getSiteId())
        );
    }


}

